﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

namespace Common.Util
{
    public class ClassUtil
    {
        /// <summary>
        /// 根据类名创建实例
        /// </summary>
        /// <param name="file"></param>
        public static object CreateInstance(string classname)
        {
            object instance = null;
            int dllPathPos = classname.LastIndexOfAny(new char[] { '.' });

            if (dllPathPos > 0)
            {
                string dllpath = classname.Substring(0, dllPathPos);
                Assembly asm = Assembly.Load(dllpath);  //  程序集名
                instance = asm.CreateInstance(classname);    //类名
            }

            return instance;
        }

        /// <summary>
        /// 根据类名创建实例
        /// </summary>
        /// <param name="file"></param>
        public static object CreateInstance(string dllname,string classname)
        {
            object instance = null;
            Assembly asm = Assembly.Load(dllname);  //  程序集名
            instance = asm.CreateInstance(classname);    //类名

            return instance;
        }

        public static object CreateInstanceByFilePath(string dllPath, string classname)
        {
            object instance = null;
            Assembly asm = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + dllPath);  //  程序集名
            instance = asm.CreateInstance(classname);    //类名

            return instance;
        }
             
    }
}
